package com.sunfeng.chap5.selectkey;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * 判断注册转状态
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 必须将serverSocketChannel 设置为阻塞的 不然会出现异常
        serverSocketChannel.configureBlocking(false);
        ServerSocket serverSocket = serverSocketChannel.socket();

        serverSocket.bind(new InetSocketAddress("localhost",8888));
        System.out.println("isRegistered="+serverSocketChannel.isRegistered());//isRegistered=false

        // 核心代码开始
        Selector selector = Selector.open();
        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        // 核心代码结束
        System.out.println("isRegistered="+serverSocketChannel.isRegistered());//isRegistered=true
        serverSocket.close();
        serverSocketChannel.close();
    }
}
